package com.xiaomei.manager.web.controller;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.xiaomei.manager.model.Extension;


import com.xiaomei.manager.service.IExtensionService;


import com.xiaomei.manager.web.dto.ExtensionDto;
import com.xiaomei.manager.web.page.Page;

@Controller
@RequestMapping("/extension")
public class ExtensionController {
	@Autowired
	private  IExtensionService iExtensionService;
	@RequiresPermissions("extension:view")
	@RequestMapping()
	public String list(Model model,ExtensionDto dto,Page<Extension> page){
		iExtensionService.selectByParamMap(dto, page);
		model.addAttribute("dto", dto);
		model.addAttribute("page",page);
		return "extension/list";
	}
	
	/**
     * 添加
     * @param model
     * @return
     */
	 @RequiresPermissions("extension:create")
	    @RequestMapping( value = "/create",method = RequestMethod.GET)
	    public String showCreateForm(Model model) {	        
	        model.addAttribute("exp", new Extension());
	        model.addAttribute("op", "新增");
	        return "extension/edit";
	    }
	 @RequiresPermissions("extension:create")
	    @RequestMapping(value = "/create",method = RequestMethod.POST)
	    public String create(Extension exp , RedirectAttributes redirectAttributes) {     	
		 	iExtensionService.createExtension(exp);
	        redirectAttributes.addFlashAttribute("msg", "新增成功");
	        return "redirect:/extension";
	    }
	 /**
	     * 更新Extension图内容
	     * @param id
	     * @param model
	     * @return
	     */
	    @RequiresPermissions("extension:update")
	    @RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	    public String showUpdateForm(@PathVariable("id") Integer id, Model model) {
	        model.addAttribute("exp", iExtensionService.findOne(id));
	        model.addAttribute("op", "修改");
	        return "extension/edit";
	    }

	    @RequiresPermissions("extension:update")
	    @RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
	    public String update(Extension exp,  @PathVariable("id") Integer id, RedirectAttributes redirectAttributes, HttpServletRequest request){	    		    
	        iExtensionService.updateExtension(exp);
	        redirectAttributes.addFlashAttribute("msg", "修改成功");
	        return "redirect:/extension";
	    }
	    /**
	     * 删除n
	     * @param id
	     * @param model
	     * @return
	     */
	    @RequiresPermissions("extension:delete")
	    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	    public String showDeleteForm(@PathVariable("id") Integer id, Model model) {	       
	    	model.addAttribute("exp", iExtensionService.findOne(id));
	        model.addAttribute("op", "删除");
	        return "extension/edit";
	    }
	    
	    
	    
	    @RequiresPermissions("extension:delete")
	    @RequestMapping(value="/{id}/delete" ,method=RequestMethod.POST)
	    public String delete(@PathVariable("id") Integer id,RedirectAttributes redirectAttributes ){
	    	 // Extension exp = iExtensionService.findOne(id);
		        iExtensionService.deleteExtension(id);
		        redirectAttributes.addFlashAttribute("msg", "删除成功");
		        return "redirect:/extension";
	    }
	
	
}
